﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
	public class XinNghiPhepBLL
	{
		QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
		public List<PHEP> DSDonXinPhep()
		{
			return DB.PHEPs.ToList();
		}

		public List<DGVLOCPHEP> Test(string _Context, int _MaPB)
		{
			if (_Context == "DSChoDuyet")
			{
                List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where a.TrangThaiDuyet == 0 && !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                                 //&& a.NHANVIEN.MaPB == _MaPB 
                                             select new DGVLOCPHEP()
                                             {
                                                 MaPhep = a.MaPhep,
                                                 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
                                                 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
                                                 LyDo = a.LyDo,
                                                 TuNgay = a.TuNgay ?? DateTime.MaxValue,
                                                 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
                                                 FileKemTheo = a.FileKemTheo,
                                                 NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                 ChuThich = a.ChuThich,
                                                 LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
                                             }).ToList();
                
				return ChucNang;
			}
			if (_Context == "DSDaDuyet")
			{
                List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where a.TrangThaiDuyet == 1 && !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                                 //&& a.NHANVIEN.MaPB == _MaPB 
                                             select new DGVLOCPHEP()
                                             {
                                                 MaPhep = a.MaPhep,
                                                 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
                                                 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
                                                 LyDo = a.LyDo,
                                                 TuNgay = a.TuNgay ?? DateTime.MaxValue,
                                                 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
                                                 FileKemTheo = a.FileKemTheo,
                                                 NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                 ChuThich = a.ChuThich,
                                                 LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
                                             }).ToList();
				return ChucNang;      
			}

			if (_Context == "DSKhongDuyet")
			{
                List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where a.TrangThaiDuyet == 2 && !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                             //&& a.NHANVIEN.MaPB == _MaPB 
                                             select new DGVLOCPHEP()
                                             {
                                                 MaPhep = a.MaPhep,
                                                 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
                                                 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
                                                 LyDo = a.LyDo,
                                                 TuNgay = a.TuNgay ?? DateTime.MaxValue,
                                                 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
                                                 FileKemTheo = a.FileKemTheo,
                                                 NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                 ChuThich = a.ChuThich,
                                                 LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
                                             }).ToList();
					return ChucNang;     
			}
			else // Show TatCa
			{
				List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                             // && a.NHANVIEN.MaPB == _MaPB  
											 select new DGVLOCPHEP()
											 {
												 MaPhep = a.MaPhep,
												 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
												 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
												 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
												 LyDo = a.LyDo,
												 TuNgay = a.TuNgay ?? DateTime.MaxValue,
												 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
												 FileKemTheo = a.FileKemTheo,
												 NgayTao = a.NgayTao ?? DateTime.MaxValue,
												 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
												 ChuThich = a.ChuThich,
                                                 LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
											 }).ToList();
				return ChucNang;                  
			}
		}

        public List<DGVLOCPHEP> DSPhepCaNhan(int _MaNV, int _Context)
        { 
            if (_Context == 1)
            {
                List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where a.TrangThaiDuyet == 1 && a.MaNV == _MaNV
                                             select new DGVLOCPHEP()
                                             {
                                                 MaPhep = a.MaPhep,
                                                 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
                                                 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
                                                 LyDo = a.LyDo,
                                                 TuNgay = a.TuNgay ?? DateTime.MaxValue,
                                                 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
                                                 FileKemTheo = a.FileKemTheo,
                                                 NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                 ChuThich = a.ChuThich,
                                                 TrangThaiDoc = a.TrangThaiDoc ?? 1
                                             }).OrderByDescending(x => x.MaPhep).ToList();
                return ChucNang;
            }
            if (_Context == 0)
            {
                List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where a.TrangThaiDuyet == 0 && a.MaNV == _MaNV
                                             select new DGVLOCPHEP()
                                             {
                                                 MaPhep = a.MaPhep,
                                                 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
                                                 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
                                                 LyDo = a.LyDo,
                                                 TuNgay = a.TuNgay ?? DateTime.MaxValue,
                                                 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
                                                 FileKemTheo = a.FileKemTheo,
                                                 NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                 TrangThaiDoc = a.TrangThaiDoc ?? 1,
                                                 ChuThich = a.ChuThich,
                                             }).OrderByDescending(x => x.MaPhep).ToList();
                return ChucNang;
            }
            if (_Context == 2)
            {
                List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where a.TrangThaiDuyet == 2 && a.MaNV == _MaNV
                                             select new DGVLOCPHEP()
                                             {
                                                 MaPhep = a.MaPhep,
                                                 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
                                                 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
                                                 LyDo = a.LyDo,
                                                 TuNgay = a.TuNgay ?? DateTime.MaxValue,
                                                 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
                                                 FileKemTheo = a.FileKemTheo,
                                                 NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                 ChuThich = a.ChuThich,
                                                 TrangThaiDoc = a.TrangThaiDoc ?? 1
                                             }).OrderByDescending(x => x.MaPhep).ToList();
                return ChucNang;
            }
            else // Show TatCa
            {
                List<DGVLOCPHEP> ChucNang = (from a in DB.PHEPs
                                             orderby a.MaPhep descending
                                             where a.MaNV == _MaNV
                                             select new DGVLOCPHEP()
                                             {
                                                 MaPhep = a.MaPhep,
                                                 MaNV = a.MaNV ?? int.MaxValue,
                                                 HoTen = a.NHANVIEN.HoTen,
                                                 MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                 MaLoaiPhep = a.MaLoaiPhep ?? int.MaxValue,
                                                 LyDo = a.LyDo,
                                                 TuNgay = a.TuNgay ?? DateTime.MaxValue,
                                                 DenNgay = a.DenNgay ?? DateTime.MaxValue,
                                                 SoBuoiNghi = a.SoNgayNghi ?? float.MaxValue,
                                                 FileKemTheo = a.FileKemTheo,
                                                 NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                 TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                 ChuThich = a.ChuThich,
                                                 TrangThaiDoc = a.TrangThaiDoc ?? 1
                                             }).OrderByDescending(x => x.MaPhep).ToList();
                return ChucNang; 
            }
        }

        public List<DGVPHEPCONGTAC> lstPhepCongTac(string _Context, int _MaPB)
        {
            if (_Context == "DSChoDuyet")
            {
                List<DGVPHEPCONGTAC> ChucNang = (from a in DB.PHEPCONGTACs
                                                 orderby a.MaPhepCT descending
                                                 where a.TrangThaiDuyet == 0 && !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                                 select new DGVPHEPCONGTAC()
                                                 {
                                                     MaPhepCT = a.MaPhepCT,
                                                     MaNV = a.MaNV ?? int.MaxValue,
                                                     HoTen = a.NHANVIEN.HoTen,
                                                     MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                     ChiTiet = a.ChiTiet,
                                                     NgayDi = a.NgayDi ?? DateTime.MaxValue,
                                                     NgayLamViecLai = a.NgayLamViecLai ?? DateTime.MaxValue,
                                                     SoNgayCT = a.SoNgayCT ?? float.MaxValue,
                                                     NguoiDuyet = a.NguoiDuyet ?? int.MaxValue,
                                                     TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                     ChuThich = a.ChuThich,
                                                     NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                     NgayDuyet = a.NgayDuyet ?? DateTime.MaxValue,
                                                     LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
                                                 }).ToList();
                return ChucNang;
            }
            if (_Context == "DSDaDuyet")
            {
                List<DGVPHEPCONGTAC> ChucNang = (from a in DB.PHEPCONGTACs
                                                 orderby a.MaPhepCT descending
                                                 where a.TrangThaiDuyet == 1 && !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                                 select new DGVPHEPCONGTAC()
                                                 {
                                                     MaPhepCT = a.MaPhepCT,
                                                     MaNV = a.MaNV ?? int.MaxValue,
                                                     HoTen = a.NHANVIEN.HoTen,
                                                     MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                     ChiTiet = a.ChiTiet,
                                                     NgayDi = a.NgayDi ?? DateTime.MaxValue,
                                                     NgayLamViecLai = a.NgayLamViecLai ?? DateTime.MaxValue,
                                                     SoNgayCT = a.SoNgayCT ?? float.MaxValue,
                                                     NguoiDuyet = a.NguoiDuyet ?? int.MaxValue,
                                                     TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                     ChuThich = a.ChuThich,
                                                     NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                     NgayDuyet = a.NgayDuyet ?? DateTime.MaxValue,
                                                     LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
                                                 }).ToList();
                return ChucNang;
            }
            if (_Context == "DSKhongDuyet")
            {
                List<DGVPHEPCONGTAC> ChucNang = (from a in DB.PHEPCONGTACs
                                                 orderby a.MaPhepCT descending
                                                 where a.TrangThaiDuyet == 2 && !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                                 select new DGVPHEPCONGTAC()
                                                 {
                                                     MaPhepCT = a.MaPhepCT,
                                                     MaNV = a.MaNV ?? int.MaxValue,
                                                     HoTen = a.NHANVIEN.HoTen,
                                                     MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                     ChiTiet = a.ChiTiet,
                                                     NgayDi = a.NgayDi ?? DateTime.MaxValue,
                                                     NgayLamViecLai = a.NgayLamViecLai ?? DateTime.MaxValue,
                                                     SoNgayCT = a.SoNgayCT ?? float.MaxValue,
                                                     NguoiDuyet = a.NguoiDuyet ?? int.MaxValue,
                                                     TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                     ChuThich = a.ChuThich,
                                                     NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                     NgayDuyet = a.NgayDuyet ?? DateTime.MaxValue,
                                                     LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
                                                 }).ToList();
                return ChucNang;
            }
            else
            {
                List<DGVPHEPCONGTAC> ChucNang = (from a in DB.PHEPCONGTACs
                                                 orderby a.MaPhepCT descending
                                                 where !a.NHANVIEN.CT_ROLEs.Any(r => r.MaRole == 1)
                                                 select new DGVPHEPCONGTAC()
                                                 {
                                                     MaPhepCT = a.MaPhepCT,
                                                     MaNV = a.MaNV ?? int.MaxValue,
                                                     HoTen = a.NHANVIEN.HoTen,
                                                     MaPB = a.NHANVIEN.MaPB ?? int.MaxValue,
                                                     ChiTiet = a.ChiTiet,
                                                     NgayDi = a.NgayDi ?? DateTime.MaxValue,
                                                     NgayLamViecLai = a.NgayLamViecLai ?? DateTime.MaxValue,
                                                     SoNgayCT = a.SoNgayCT ?? float.MaxValue,
                                                     NguoiDuyet = a.NguoiDuyet ?? int.MaxValue,
                                                     TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                                     ChuThich = a.ChuThich,
                                                     NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                                     NgayDuyet = a.NgayDuyet ?? DateTime.MaxValue,
                                                     LaTruongPhongBan = a.NHANVIEN.CT_ROLEs.Any(b => b.MaRole == 2)
                                                 }).ToList();
                return ChucNang;
            }
        }
        
        
        public void TaoDonXinPhep(PHEP dxp)
		{
			DB.PHEPs.InsertOnSubmit(dxp);
			DB.SubmitChanges();
		}
		public List<PHEP> ThongTinDXP(int MaPhep, int MaPB, int MaNV)
		{
            if (MaNV != 0)
            {
                return DB.PHEPs.Where(mp => mp.MaPhep == MaPhep && mp.MaNV == MaNV).ToList();    
            }
            else
            {
                //Tất cả
                return DB.PHEPs.Where(mp => mp.MaPhep == MaPhep).ToList();
            }
		}
        public List<PHEPCONGTAC> ThongTinPhepCT(int MaPhepCT, int MaPB)
        {
            return DB.PHEPCONGTACs.Where(mp => mp.MaPhepCT == MaPhepCT).ToList();
        }
		public void DuyetDonXinPhep(PHEP _dxp)
		{
			PHEP dxp = DB.PHEPs.Single(p => p.MaPhep == _dxp.MaPhep);
			dxp.TrangThaiDuyet = 1;
			dxp.ChuThich = _dxp.ChuThich;
            dxp.NguoiDuyet = _dxp.NguoiDuyet;
            if (_dxp.TrangThaiDoc != null)
            {
                dxp.TrangThaiDoc = _dxp.TrangThaiDoc;
            }
            else { }
			DB.SubmitChanges();
		}
        public void DuyetPhepCT(PHEPCONGTAC _pct)
        {
            PHEPCONGTAC pct = DB.PHEPCONGTACs.Single(p => p.MaPhepCT == _pct.MaPhepCT);
            pct.TrangThaiDuyet = 1;
            pct.ChuThich = _pct.ChuThich;
            pct.NguoiDuyet = _pct.NguoiDuyet;
            pct.NgayDuyet = _pct.NgayDuyet;
            if (_pct.TrangThaiDoc != null)
            {
                pct.TrangThaiDoc = _pct.TrangThaiDoc;
            }
            else { }
            DB.SubmitChanges();
        }
		public void KhongDuyetDonXinPhep(PHEP _dxp)
		{
			PHEP dxp = DB.PHEPs.Single(p => p.MaPhep == _dxp.MaPhep);
			dxp.TrangThaiDuyet = 2;
			dxp.ChuThich = _dxp.ChuThich;
            dxp.NguoiDuyet = _dxp.NguoiDuyet;
            dxp.NgayDuyet = _dxp.NgayDuyet;
            if (_dxp.TrangThaiDoc != null)
            {
                dxp.TrangThaiDoc = _dxp.TrangThaiDoc;
            }
            else { }
			DB.SubmitChanges();
		}
        public void KhongDuyetPhepCT(PHEPCONGTAC _pct)
        {
            PHEPCONGTAC pct = DB.PHEPCONGTACs.Single(p => p.MaPhepCT == _pct.MaPhepCT);
            pct.TrangThaiDuyet = 2;
            pct.ChuThich = _pct.ChuThich;
            pct.NguoiDuyet = _pct.NguoiDuyet;
            if (_pct.TrangThaiDoc != null)
            {
                pct.TrangThaiDoc = _pct.TrangThaiDoc;
            }
            else { }
            DB.SubmitChanges();
        }
		public string KiemTraTonTai(int MaPhep)
		{
			if (DB.PHEPs.Any(p => p.MaPhep == MaPhep))
				return "Mã đơn xin phép";
			return null;
		}
		public LOAIPHEP LayLoaiPhep(int _MaLoaiPhep)
		{
			LOAIPHEP P = DB.LOAIPHEPs.Single(a => a.MaLoaiPhep == _MaLoaiPhep);
			return P;
		}

        public PHEP layThongTinPhep(int _MaPhep)
        {
            PHEP P = new PHEP();
            P = DB.PHEPs.SingleOrDefault(a => a.MaPhep == _MaPhep);
            if (P != null)
                return P;
            return null;
        }
        public PHEPCONGTAC layThongTinPhepCT(int _MaPhep)
        {
            PHEPCONGTAC P = new PHEPCONGTAC();
            P = DB.PHEPCONGTACs.SingleOrDefault(a => a.MaPhepCT == _MaPhep);
            if (P != null)
                return P;
            return null;
        }
        public void CapNhatPhepThuongNien(CT_PHEPTHUONGNIEN ptn)
        {
            //DB.CT_PHEPTHUONGNIENs.InsertOnSubmit(ptn);
            CT_PHEPTHUONGNIEN P = DB.CT_PHEPTHUONGNIENs.SingleOrDefault(a => a.MaNV == ptn.MaNV && a.Nam == ptn.Nam);
            P.SoNgayNghiConLai = ptn.SoNgayNghiConLai;
            DB.SubmitChanges();
        }
        public void ThemPhepThuongNien(CT_PHEPTHUONGNIEN ptn)
        {
            DB.CT_PHEPTHUONGNIENs.InsertOnSubmit(ptn);
            DB.SubmitChanges();
        }
        public CT_PHEPTHUONGNIEN LayThongTinPhepThuongNienConLai(int _MaNV, DateTime _dtmNam)
        {
            CT_PHEPTHUONGNIEN P = DB.CT_PHEPTHUONGNIENs.SingleOrDefault(p => p.MaNV == _MaNV && p.Nam == _dtmNam.Year);
            return P;
        }

        public void Sua(PHEP _p)
        {
            PHEP p = DB.PHEPs.SingleOrDefault(a => a.MaPhep == _p.MaPhep);
            if (p == null)
            { }
            else
            {
                if (_p.TrangThaiDoc != null)
                {
                    p.TrangThaiDoc = _p.TrangThaiDoc;
                }
                DB.SubmitChanges();
            }
        }

        public int DemThongBao(int _maNV, int _maPB, int _mattd)
        {
            if (_maPB == 0)
            {
                List<PHEP> ls = (from a in DB.PHEPs
                                 where a.MaNV == _maNV && a.TrangThaiDuyet == _mattd && a.TrangThaiDoc == 0
                                 select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
            else
            {
                List<PHEP> ls = (from a in DB.PHEPs
                                 where a.NHANVIEN.MaPB == _maPB && a.TrangThaiDuyet == _mattd
                                 select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
        }
	}
}
